package com.gandong8.es.web.controller;

import com.gandong8.es.tool.core.EsQuery;
import com.gandong8.es.web.domain.common.Result;
import com.gandong8.es.web.domain.esindex.TestIndex;
import com.gandong8.es.web.service.TestIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;

@Slf4j
@Controller
@RequestMapping(value = "/test")
public class TestIndexController{

    @Autowired
    TestIndexService service;

    //http://localhost:8066/test/index/create
    @RequestMapping(value = "/index/create")
    @ResponseBody
    public Result indexCreate() {
        return Result.run(service.indexCreate());
    }

    @RequestMapping(value = "/index/delete")
    @ResponseBody
    public Result indexDelete() {
        return Result.run(service.indexDelete());
    }

    //http://localhost:8066/test/doc/add?id=1
    @RequestMapping(value = "/doc/add")
    @ResponseBody
    public Result docAdd(String id)  {
        TestIndex obj = new TestIndex(id);
        obj.setAddTime(new Date());
        obj.setEnv("beta");
        obj.setPath("/doc/add");
        obj.setCost(500L);
        obj.setResult("success");
        return Result.run(service.add(obj));
    }

    //http://localhost:8066/test/doc/update?id=1
    @RequestMapping(value = "/doc/update")
    @ResponseBody
    public Result docUpdate(String id){
        TestIndex obj = new TestIndex(id);
        obj.setCost(200L);
        obj.setEditTime(new Date());
        return Result.run(service.update(obj));
    }

    //http://localhost:8066/test/doc/get?id=1
    @RequestMapping(value = "/doc/get")
    @ResponseBody
    public Result docGet(String id) {
        TestIndex obj = new TestIndex(id);
        return Result.success(service.get(obj));
    }

    //http://localhost:8066/test/doc/del?id=1
    @RequestMapping(value = "/doc/del")
    @ResponseBody
    public Result docDelete(String id) {
        TestIndex obj = new TestIndex(id);
         return Result.run(service.delete(obj));
    }

    //http://localhost:8066/test/doc/search
    @RequestMapping(value = "/doc/search")
    @ResponseBody
    public Result docSearch() throws IOException {
        TestIndex testIndex = new TestIndex();
        //testIndex.setId("1");
        EsQuery<TestIndex> esQuery = new EsQuery<>(testIndex);
        return Result.success(service.search(esQuery));
    }
}
